package com.example.controller;

import com.example.mapper.one.UserMapper;
import com.example.mapper.two.Neo4j;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.TokenService;
import com.example.service.UserService;
import com.example.util.ResultUtil;
import com.example.util.token.PassToken;
import com.example.util.token.UserLoginToken;
import com.example.util.vCode.IVerifyCodeGen;
import com.example.util.vCode.SimpleCharVerifyCodeGenImpl;
import com.example.util.vCode.VerifyCode;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController{

    @Resource
    private Neo4j neo4j;
    @Resource
    private UserService userService;
    @Resource
    private TokenService tokenService;
    @Resource
    private UserMapper userMapper;

    /*登录*/
    @PassToken
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestParam String username, @RequestParam String password, @RequestParam String vCode, HttpServletRequest request,HttpServletResponse response){
        JSONObject jsonObject = new JSONObject();
        HttpSession session = request.getSession(true);
        String code = (String)session.getAttribute("VerifyCode");
        Result result;
        if(!vCode.toLowerCase().equals(code.toLowerCase())){
            result = ResultUtil.error(500,"验证码错误");
        }
        else{
            User user = userService.queryUser(username,password);
            if(user == null){
                result = ResultUtil.error(500,"账号或密码错误");
            }
            else{
                String token = tokenService.getToken(user);
                Map resultMap = new HashMap();
                resultMap.put("token",token);
                resultMap.put("power",user.getJurisdiction());
                result = ResultUtil.success(resultMap);
            }
        }
        return result;
    }

    @PassToken
    @RequestMapping("/verifyCode")
    public String verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
//            //将VerifyCode绑定session
            HttpSession session = request.getSession(true);
            session.setAttribute("VerifyCode",code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
//            System.out.println(session.getAttribute("VerifyCode"));
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
            return code;
        }
        catch (IOException e) {
            return "";
        }
    }

    @RequestMapping("/getAllUser")
    public Result getAllUser() {
       Result result;
       List<User> userList = userMapper.getAllUser();
       result = ResultUtil.success(userList);
       return result;
    }

    @RequestMapping("/delUser")
    public Result delUser(@RequestParam Integer userId) {
        Result result;
        int isDel = userMapper.delUser(userId);
        if(isDel > 0){
            result = ResultUtil.success("删除成功");
        } else {
            result = ResultUtil.error(500,"删除失败");
        }
        return result;
    }

    @RequestMapping("/addUser")
    public Result addUser(@RequestParam String userName, @RequestParam String password) {
        Result result;
        int isDel = userMapper.addUser(userName,password);
        if(isDel > 0){
            result = ResultUtil.success("删除成功");
        } else {
            result = ResultUtil.error(500,"删除失败");
        }
        return result;
    }
}
